package com.ultron.app;

import com.ultron.app.config.myBatisPlus.MyMetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;

@Slf4j
@SpringBootApplication
@ServletComponentScan
public class UltronDemoApplication {

    @Value("${httpport}")
    private int httpport;

    @Autowired
    private MyMetaObjectHandler myMetaObjectHandler;

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(UltronDemoApplication.class, args);
        // 检查容器中是否存在 MyMetaObjectHandler 实例
        MyMetaObjectHandler handler = context.getBean(MyMetaObjectHandler.class);
        // 若打印true，说明已被管理
        log.info("MyMetaObjectHandler 是否存在：" + (handler != null));
        log.info("=======================启动成功=======================");
    }

    /**
     * 支持双协议
     * @return
     */
    @Bean
    public ServletWebServerFactory serverFactory() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.addAdditionalTomcatConnectors(createStandardConnector());
        return tomcat;
    }

    /**
     * 配置http
     *
     * @return
     */
    private Connector createStandardConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setPort(httpport);
        return connector;
    }
}
